<?php
class contact extends Admin {

    private $name = "联系人";

    public function index( $c, $a ) {
        $this->table = strtolower( $c );

        $where = array( G( 'k' ) => G( 'v' ) );
        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "supplierName like '%{$key}%'" ) : '';
        }


        $result = $this->Paged( G( 'page' ), $this->page_size )->findAll( $where, ' id desc' );

        $pager = $result['page'];
        $result = $result['result'];
        include $this->display( $c );
    }

    public function search($c,$a)
    {
        $sql = "SELECT * FROM xbl_contact AS e INNER JOIN xbl_supplier AS d ON e.supplierId = d.id WHERE d.name ='".$_GET[key]."'";
        $result = $this->findSql($sql);

        if(empty($result))
            $result = 0;

        include $this->display( $c );

    }
    public function edit( $c, $a ) {
        if ( !$id = intval( G( 'id' ) ) ) {
            $this->error();
            exit;
        }

        $this->table =  strtolower( $c ); // 数据库表名
        $result = $this->find( array( 'id' => $id ) ); // 查找数据
        include $this->display( $c ); // 加载模板
    }

    public function add( $c, $a ) {
        $result['supplierName'] = G('supplierName');
        include $this->display( $c );
    }

    # 删除商品
    public function del( $c ) {
        if ( !$id = intval( G( "id" ) ) ) {
            exit( '非法删除商品' );
        }

        #删除商品
        $this->table = strtolower( $c );
        $msg = $this->delete( array( "id" => $id ) ) ? 'success' : 'failed';


        exit( $msg );
    }

    public function save($c) {
        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        # 注意 表单字段必须跟数据库表字段名一样
        $this->table = strtolower( $c ); // 数据库表名
        if ( $id = intval( G( "id" ) ) ) {
            $msg = $this->update( array( 'id' => $id ), $rows ) ? 'success' : 'failed';  //更新供货商
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商
        }
        exit( $msg );
    }
}